from Bio.Align import substitution_matrices
from align import SubstMatrix

class SubstMatrix_Bio(SubstMatrix):
    def __init__(self,sm_bio_type:str="BLOSUM62"):
        self.sm_table = substitution_matrices.load(sm_bio_type)
        self.alphabet = list(self.sm_table.alphabet)
        self.sm = dict()
        self.build()
    
    def build(self):
        for i,from_word in enumerate(self.alphabet):
            for j,to_word in enumerate(self.alphabet):
                self.sm[from_word+to_word] = int(self.sm_table[i,j])

    def get_dict(self):
        return self.sm
    
    def write_file(self,filename:str):
        with open(filename,"w") as f:
            f.write('\t'.join(self.alphabet)+"\n")
            for i in range(24):
                f.write('\t'.join(map(lambda v :str(int(v)),self.sm_table[i,:])) + "\n")
